package com.core.gig.web.action;

import java.util.Collections;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.core.as.ASException;
import com.core.gig.as.ASCentroCusto;
import com.core.gig.as.ASConfiguracao;
import com.core.gig.as.ASCusto;
import com.core.gig.as.ASFactory;
import com.core.gig.dto.RecursoHumanoDTO;
import com.core.gig.util.GIGUtil;

/**
 * @author Lucas Ceni
 *
 */
public class RecursoHumanoAction
		extends GIGAction
{
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		return novo(mapping, form, request, response);
	}
	
	public ActionForward novo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		clearForm(form);
		return incluir(mapping, form, request, response);
	}

	public ActionForward incluir(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		setScreenType(request, SCR_INCLUDE);
		
		try
		{
			ASConfiguracao asConfiguracao = ASFactory.getInstance().getASConfiguracao();
			ASCentroCusto asCentroCusto = ASFactory.getInstance().getASCentroCusto();
			request.setAttribute("centrosCusto", asCentroCusto.listarCentrosCusto());
			
			request.setAttribute("encargos", asConfiguracao.recuperarTotalEncargos());
		}
		catch (ASException e)
		{
			request.setAttribute("centrosCusto", Collections.EMPTY_LIST);
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		return mapping.findForward(FWD_DEFAULT);
	}
	
	public ActionForward salvar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		RecursoHumanoDTO dto = new RecursoHumanoDTO();
		GIGUtil.copyProperties(dto, form);
		
		try
		{
			ASCusto as = ASFactory.getInstance().getASCusto();
			as.salvarRecursoHumano(dto);
			
			if (id(form) == 0)
			{
				addMessage(request, "global.msg.sucess.inclusion", ICO_OK);
				dyna(form).set("id", "");
			}
			else
			{
				addMessage(request, "global.msg.sucess.alteration", ICO_OK);
				return novo(mapping, form, request, response);
			}
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		return incluir(mapping, form, request, response);
	}
	
	public ActionForward listar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		try
		{
			ASCusto asCusto = ASFactory.getInstance().getASCusto();
			request.setAttribute("recursosHumanos", asCusto.listarRecursosHumanos());
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		return mapping.findForward(FWD_LIST);
	}
	
	public ActionForward excluir(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		int id = id(form);
		
		RecursoHumanoDTO dto = new RecursoHumanoDTO();
		dto.setId(id);
		
		try
		{
			ASCusto as = ASFactory.getInstance().getASCusto();
			as.excluirRecursoHumano(dto);
			addMessage(request, "global.msg.sucess.exclusion", ICO_OK);
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		return listar(mapping, form, request, response);
	}
	
	public ActionForward alterar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		RecursoHumanoDTO dto = new RecursoHumanoDTO();
		dto.setId(id(form));
		
		try
		{
			ASCusto as = ASFactory.getInstance().getASCusto();
			ASCentroCusto asCentroCusto = ASFactory.getInstance().getASCentroCusto();
			ASConfiguracao asConfiguracao = ASFactory.getInstance().getASConfiguracao();
			
			dto = as.recuperarRecursoHumano(dto);
			
			request.setAttribute("encargos", asConfiguracao.recuperarTotalEncargos());
			request.setAttribute("centrosCusto", asCentroCusto.listarCentrosCusto());
			
			GIGUtil.copyProperties(form, dto);
			dyna(form).set("salario", GIGUtil.formatNumber(dto.getSalario()));
			dyna(form).set("dtNascimento", GIGUtil.formatDate(dto.getDtNascimento()));
			
			setScreenType(request, SCR_MODIFY);
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
			return listar(mapping, form, request, response);
		}
		return mapping.findForward(FWD_DEFAULT);
	}
}